package com.beehoo.dcportal.utils;

import java.util.List;

import com.beehoo.dcportal.common.Constant;
import com.beehoo.dcportal.model.FunctionBean;

public class FunctionTree {

	public static String buildFunctionTree(List<FunctionBean> functionList) {
		StringBuffer buf = new StringBuffer();
		if (functionList != null && functionList.size() > 0) {
			buf.append("foldersTree = gFld(\" class=root>系统菜单\",\"\",\""+Constant.DEFAULT_IMG_ROOT+"\")\n");
			for(FunctionBean functionBean : functionList){
				String fid = CommonUtils.nvlToString(functionBean.getFunctionId());
				String parentId = CommonUtils.nvlToString(functionBean.getParentId());
				if(parentId.equals("0")){
					String fName = CommonUtils.nvlToString(functionBean.getName());
					String url = CommonUtils.nvlToString(functionBean.getUrl());
					buf.append("aux1 = insFld(foldersTree, gFld(\" class=folder>"
							+ fName
							+ "\",\""
							+ url
							+ "\",\""
							+ Constant.DEFAULT_IMG_FOLDER + "\"))\n");
					for(FunctionBean fBean : functionList){
						String fParentId = CommonUtils.nvlToString(fBean.getParentId());
						if(fParentId.equals(fid)){
							String ffName = CommonUtils.nvlToString(fBean.getName());
							String ffUrl = CommonUtils.nvlToString(fBean.getUrl()); 
							buf.append("insDoc(aux1, gLnk(0, \" class=doc>"
									+ ffName + "\", \"" + ffUrl + "\",\""
									+ Constant.DEFAULT_IMG_DOCPIC + "\"))\n");
						}
					}
				}
			}
		} else {
			buf.append("foldersTree = gFld(\" class=root>您没有足够权限\",\"\",\""+Constant.DEFAULT_IMG_ROOT+"\")\n");
		}
		return buf.toString();
	}
}
